import {request, METHOD, removeAuthorization} from '@/utils/request'
import crypto from 'crypto'
import {AUTH_TYPE} from "../utils/request";

/**
 * 注册验证用户名是否唯一
 */
 export async function isUserNameValid(userName) {
  return request('/agri/api/common/tenant/checkLoginNameUnique?loginNameTemp=' + userName, METHOD.GET)
}

/**
 * 注册验证手机号是否唯一
 */
 export async function isMobilePhoneValid(mobilePhone) {
  return request('/agri/api/common/tenant/checkMobilePhoneUnique?mobilePhone=' + mobilePhone, METHOD.GET)
}

/**
 * 发送手机验证码
 */
 export async function getMobilePhoneYZM(userName,data) {
  //  手机号码sha1加密
  var sha1 = crypto.createHash('sha1')
  sha1.update(data.phoneNumber.split("").reverse().join(""))
  const phoneNumber_sha = sha1.digest('hex')
  data.sign = phoneNumber_sha.substring(10,20)
  data.tenantCode = 'T000010'
  return request('/agri/api/common/tenant/sendVerifyCodeForRegister?loginNameTemp=' + userName, METHOD.PUT,data)
}
//修改手机号发送验证码
export async function amendPhoneYZM(data) {
  //  手机号码sha1加密
  var sha1 = crypto.createHash('sha1')
  sha1.update(data.phoneNumber.split("").reverse().join(""))
  const phoneNumber_sha = sha1.digest('hex')
  data.sign = phoneNumber_sha.substring(10,20)
  data.isSend4NewPhone = 1
    data.tenantCode = 'T000010'
  return request('/agri/api/common/tenant/sendVerifyCodeForModifyPhone', METHOD.POST,data)
}
export async function confirmChangeMobile(data) {
  return request('/agri/api/common/tenant/updateMobilePhone', METHOD.POST,data)
}

/**
 * 用户注册
 */
 export async function userRegister(userName,{ loginName, password, mobilePhone, verifyCode, roleCode}) {
  //  密码sha1加密
  var sha1 = crypto.createHash('sha1')
  sha1.update(password)
  const phoneNumber_sha = sha1.digest('hex')
  password = phoneNumber_sha
  const data = {
    loginName,
    password,
    mobilePhone,
    verifyCode,
    roleCode,
    tenantCode:'T000010'
  }
  return request('/agri/api/common/register/register4Normal?loginNameTemp=' + userName, METHOD.POST,data)
}

/**
 * 企业信息录入,企业种类和企业属性字段请求(企业类型和企业属性下拉列表选项配置)
 */
 export async function businessInfoOfType(pCode) {
  return request('/agri/api/common/businessData/queryList?pCode=' + pCode, METHOD.GET)
}
// 专家分类
export async function zjInfoOfType(pCode) {
  return request('/agri/api/common/businessData/queryList4Amis?tenantCode=T000010&pCode=' + pCode, METHOD.GET)
}
// 专家职称
export async function zjTitleOfType(pCode) {
  return request('/agri/api/common/expertTitle/queryList?tenantCode=T000010&pCode=' + pCode, METHOD.GET)
}
/**
 * 企业信息录入,企业地址选择配置(级联选择器地址options配置)
 */
 export async function businessInfoOfAdress() {
  return request('/agri/api/common/regionManage/queryList', METHOD.GET)
}
export async function registrationType() {
  return request('/agri/api/common/role/query4RegSelect?queryTenantCode=T000010', METHOD.GET)
}
export async function enterpriseDetail(loginUserId) {
  return request(`/agri/api/common/subjectInfoEnterprise/queryDetailByUserId?queryTenantCode=T000010&userId=${loginUserId}`, METHOD.GET)
}
/**
 * 用户注册下一步,企业信息录入
 */
 export async function businessInfoSave(userName,data) {
  //  密码sha1加密
  // var sha1 = crypto.createHash('sha1')
  // sha1.update(data.tenantAdminId)
  // const tenantAdminId_sha = sha1.digest('hex')
  // data.tenantAdminId = tenantAdminId_sha
  return request('/agri/api/common/subjectInfoEnterprise/saveBaseInfo?loginNameTemp=' + userName, METHOD.POST,data)
}
//养殖户注册下一步
export async function farmerRegistration(data) {
  console.log(data,1000)
  let params = {
    userId:data.userId,
    enterpriseName:data.yzcName,
    unifiedSocialCreditCode:data.yzcCreditCode,
    representativeName:data.yzcEnterpriseName,
    enterpriseContactPhone:data.yzcContactNumber,
    businessProvinceCode:data.yzcProvinceCode,
    businessCityCode:data.yzcCityCode,
    businessCountyCode:data.yzcCountyCode,
    businessAddress:data.yzcBusinessAddress,
    businessScope:data.yzcBusinessScope,
    idCardNumImg:data.yzcCertificateImg,
    foundDate:data.yzcDate,
    enterpriseRegisterTypeCode:'FARMERS',
    milkStationFlag:data.milkStationFlag
  }
  return request('/agri/api/common/subjectInfoEnterprise/saveBaseInfo4Farmer', METHOD.POST,params)
}
//专家注册下一步
export async function businessExperts(data) {
  console.log(data,789)
  let params = {
    userId:data.userId,
    name:data.theName,
    photo:data.headPortrait,
    sex:data.resource == 1 ? '男':'女',
    birthday:data.zjFoundDate,
    typeCodes:data.goodField.join(','),
    expertTitleId:data.expertsTitles,
    workUnit:data.workUnits,
    provinceCode:data.zjProvinceCode,
    cityCode:data.zjCityCode,
    countyCode:data.zjCountyCode,
    address:data.zjBusinessAddress,
    achievement:data.personalAchievement,
    expertIntroduce:data.personalProfile,
    expertTitleImage:data.businessCertificateImg,
    idCardImage:data.idCardPhoto,
    idCardNum:data.zjIdNumber,
  }
  return request('/agri/api/common/subjectInfoExpert/save', METHOD.POST,params)
}
export async function rejectExpert(loginUserId) {
  // return request('/agri/api/common/org/queryOrgTree', METHOD.GET)
  // return request('http://123.56.95.192:9999/mock/701/agri/api/common/org/queryOrgTree', METHOD.GET)
  return request(`/agri/api/common/subjectInfoExpert/queryDetailByUserId?queryTenantCode=T000010&userId=${loginUserId}`, METHOD.GET)
}
export async function enterpriseExpert(loginUserId) {
  // return request('/agri/api/common/org/queryOrgTree', METHOD.GET)
  // return request('http://123.56.95.192:9999/mock/701/agri/api/common/org/queryOrgTree', METHOD.GET)
  return request(`/agri/api/common/subjectInfoExpert/queryDetailByUserId?tenantCode=T000010&loginUserId=${loginUserId}`, METHOD.GET)
}

/**
 * 组织机构管理,左侧树状图数据请求
 */
 export async function InstitutionManageTreeData() {
  // return request('/agri/api/common/org/queryOrgTree', METHOD.GET)
  // return request('http://123.56.95.192:9999/mock/701/agri/api/common/org/queryOrgTree', METHOD.GET)
  return request('/agri/api/common/org/queryOrgTree', METHOD.GET)
}
/**
 * 岗位管理,左侧树状图数据请求
 */
 export async function postManageTreeData() {
  // return request('/agri/api/common/org/queryOrgTree', METHOD.GET)
  return request('http://123.56.95.192:9999/mock/701/agri/api/common/org/queryOrgTree', METHOD.GET)
  // return request('/agri/api/common/org/queryOrgTree', METHOD.GET)
}
/**
 * 组织机构管理
 * 根据左侧树状图的选择请求分页列表
 */
 export async function InstitutionManageListData(pid,page,perPage) {
  // return request('/agri/api/common/org/queryOrgTree', METHOD.GET)
  // TODO pid 页码 每页数量暂未填写
  return request(`/agri/api/common/org/queryListPage?pid=${pid}&page=${page}&perPage=${perPage}`, METHOD.GET)
}
/**
 * 岗位管理
 * 根据左侧树状图的选择请求分页列表
 */
//  export async function postManageListData(orgId,page,perPage) {
 export async function postManageListData() {
  // return request('/agri/api/common/org/queryOrgTree', METHOD.GET)
  // TODO pid 页码 每页数量暂未填写
  // TODO 暂时先用假数据
  // return request(`/agri/api/common/post/queryListPage?orgId=${orgId}&page=${page}&perPage=${perPage}`, METHOD.GET)
  return request(`http://123.56.95.192:9999/mock/701/agri/api/common/post/queryListPage`, METHOD.GET)
}

/**
 * 右侧table表格新增/修改请求
 */
 export async function InstitutionManageListDataPOST(id,data) {
  //  TODO 需要对id进行加密
  return request(`/agri/api/common/org/update/${id}`, METHOD.POST,data)
}

/**
 * 岗位管理
 * 右侧table表格新增/修改请求
 */
 export async function postManageListDataPOST(id,data) {
  //  TODO 需要对id进行加密
  return request(`http://123.56.95.192:9999/mock/701/agri/api/common/post/update/${id}`, METHOD.POST,data)
}

/**
 * 岗位管理
 * 右侧table表格删除请求
 */
 export async function postManageListDataDelete(id) {
  //  TODO 需要对id进行加密
  return request(`http://123.56.95.192:9999/mock/701/agri/api/post/delete/${id}`, METHOD.GET)
}
/**
 * 右侧table表格启用/禁用请求
 */
 export async function InstitutionManageListDataDisabled(id,shieldVaule) {
  //  TODO 需要对id进行加密
  return request(`/agri/api/common/org/shield/${id}/${shieldVaule}`, METHOD.POST)
}

/**
 * 右侧table表格新增请求
 */
 export async function InstitutionManageListDataAdd(data) {
  //  TODO 需要对id进行加密
  return request(`http://123.56.95.192:9999/mock/701/agri/api/common/org/save`, METHOD.POST,data)
}

/**
 * 登录服务
 * @param name 账户名
 * @param password 账户密码
 * @returns {Promise<AxiosResponse<T>>}
 * 登录之前对密码进行加密
 */
export async function login(form) {
  var sha1 = crypto.createHash('sha1')
  sha1.update(form.password)
  const password_sha = sha1.digest('hex')
  form.password = password_sha
  return request('/agri/api/common/loginManage/login', METHOD.POST, form)
}

export async function queryUserTenantList(params){
    return request('/agri/api/common/userManage/queryUserTenantList', METHOD.GET, params )
}
/**
 * 获取登录验证码
 */
 export async function getLoginYZM() {
  return request('/agri/api/common/loginManage/getCode', METHOD.GET)
}

export async function getRoutesConfig(params) {
  return request('/agri/api/common/userManage/menus', METHOD.GET, params)
}

export async function loginOut() {
  return request('/agri/api/common/tenant/logout', METHOD.POST)
}

/**
 * 打印溯源码标签
 */
export async function printLable(id,number) {
  const data = {
    printNumber: number
  }
  return request(`/agri/api/sy/labelInfo/print/${id}`, METHOD.POST, data)
}

export function logout1() {
    // 清除本地存储
    window.localStorage.clear();
    window.sessionStorage.clear();
    removeAuthorization(AUTH_TYPE.BASIC);
    window.location.href = "/admin/login";
}

/**
 * 退出登录
 */
export function logout() {
  localStorage.removeItem(process.env.VUE_APP_ROUTES_KEY)
  localStorage.removeItem(process.env.VUE_APP_PERMISSIONS_KEY)
  localStorage.removeItem(process.env.VUE_APP_ROLES_KEY)
  removeAuthorization(AUTH_TYPE.BASIC)
}

/**
 * 忘记密码页面,发送手机验证码
 */
export async function forgetPasswordGetYZM(data) {
  //  手机号码sha1加密
  var sha1 = crypto.createHash('sha1')
  sha1.update(data.phoneNumber.split("").reverse().join(""))
  const phoneNumber_sha = sha1.digest('hex')
  data.sign = phoneNumber_sha.substring(10,20)
    data.tenantCode = 'T000010'
  return request('/agri/api/common/tenant/sendVerifyCodeForGetPassword', METHOD.POST,data)
}
/**
 * 忘记密码页面,校验手机验证码
 */
export async function forgetPasswordGetYZMvalid({ mobilePhone, verifyCode }) {
  const data = {
    phoneNumber:mobilePhone,
    verifyCode:verifyCode
  }
  return request('/agri/api/common/tenant/checkVerifyCodeForGetPassword', METHOD.POST,data)
}
/**
 * 忘记密码页面,修改密码
 */
export async function forgetPasswordUpdatePassword(data) {
  //  密码sha1加密
  var sha1 = crypto.createHash('sha1')
  sha1.update(data.password)
  const password_sha = sha1.digest('hex')
  data.password = password_sha
  return request('/agri/api/common/tenant/updatePasswordByPhoneNumber', METHOD.POST,data)
}

/**
 * 登录成功后,修改密码
 */
export async function loginUserUpdatePassword({oldPassword, password,confirmPassword}) {
  const data = {}
  //  old密码sha1加密  登录密码加密规则
  // var sha1 = crypto.createHash('sha1')
  // sha1.update(form.password)
  // const password_sha = sha1.digest('hex')


  var sha1 = crypto.createHash('sha1')
  sha1.update(oldPassword)
  const oldPassword_sha = sha1.digest('hex')
  data.oldPassword = oldPassword_sha

  var sha2 = crypto.createHash('sha1')
  sha2.update(password)
  const password_sha = sha2.digest('hex')
  data.password = password_sha

  var sha3 = crypto.createHash('sha1')
  sha3.update(confirmPassword)
  const confirmPassword_sha = sha3.digest('hex')
  data.confirmPassword = confirmPassword_sha
  return request('/agri/api/common/tenant/updatePassword', METHOD.POST,data)
}



export default {
  login,
  logout,
  getRoutesConfig
}
